package com.example.zz.example.animations;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import com.example.zz.example.R;
import com.example.zz.example.databinding.ActivityAnimationsBinding;

public class AnimationsActivity extends AppCompatActivity {


    private com.example.zz.example.databinding.ActivityAnimationsBinding mBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_animations);

        final ValueAnimator enterAnimator = ObjectAnimator.ofFloat(mBinding.animationTv, "alpha", 0, 1);//淡入效果
        enterAnimator.setDuration(500);
        enterAnimator.setInterpolator(new AccelerateInterpolator());

        final ValueAnimator enterAnimator2 = ObjectAnimator.ofFloat(mBinding.animationTv, "alpha", 1, 0);//淡入效果
        enterAnimator2.setDuration(500);
        enterAnimator2.setInterpolator(new AccelerateInterpolator());
        enterAnimator2.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mBinding.animationTv.setVisibility(View.GONE);
            }
        });

        enterAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        enterAnimator2.start();
                    }
                }, 3000L);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mBinding.animationTv.setVisibility(View.VISIBLE);
            }
        });

        enterAnimator.start();
    }
}